"""
Copyright (c) 2022 Huawei Technologies Co.,Ltd.

openGauss is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:

          http://license.coscl.org.cn/MulanPSL2

THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details.
"""
"""
Case Type   : 功能-功能测试
Case Name   : 插件被卸载Tab关闭
Create At   : 2023/5/25
Owner       : n@ningyali
Description :
    1. 打开基础运维-集群监控
    2. 打开业务建模-报表设计，修改某个数据流，会打开新Tab
    3. 打开插件管理页面
    4. 找到基础运维插件，点击卸载按钮，确认【结果1】
    5. 查看之前基础运维插件打开的Tab页面【结果2】
Expect      :
    1.【结果1】卸载成功
    2.【结果2】插件对应的Tab都被关闭了
History     :
"""
import os
import time
import unittest

from selenium.common import TimeoutException, InvalidArgumentException

from openGaussMigrate.common.base import Base
from openGaussMigrate.common.com_log import ComLog
from openGaussMigrate.common.com_node import Macro
from openGaussMigrate.common.com_unit_action import ComUnitAction


class Install0013(unittest.TestCase):
    def setUp(self):
        self.log = ComLog(self.__class__.__name__)
        self.log.info(f'-----{os.path.basename(__file__)} start-----')
        web = Macro('WEB_INFO')
        self.driver = ComUnitAction(web.info['driver_path'], web.info['url'])
        self.driver.open()
        self.driver.login(web.info['user'], web.info['passwd'])
        self.log.info('setup run finished')
        self.plugin_name = "base-ops"
        self.plugin_name_cn = "基础运维"
        self.plugin_path = os.path.join(os.getcwd(),
                                        'resources',
                                        'plugins',
                                        'base-ops-5.0.0-repackage.jar')

    @Base.retry(max_n=1)
    @Base.save_img_when_err(cls_name='Install0013')
    def test_case1(self):
        """插件被卸载Tab关闭"""
        self.log.info('前置操作：安装插件')
        if not self.driver.is_plugin_intalled(self.plugin_name):
            self.driver.install_plugin(self.plugin_name_cn, self.plugin_path)

        self.log.info('步骤1：打开基础运维-集群监控')
        # 通过selenium定位元素再点击失败率很高，改用js
        js = "Array.from(document.querySelectorAll('span')).find(el => " \
             "el.textContent === '基础运维').click();"
        self.driver.exec_js(js)
        time.sleep(1)
        js = "Array.from(document.querySelectorAll('span')).find(el => " \
             "el.textContent === '集群监控').click();"
        self.driver.exec_js(js)
        time.sleep(5)

        self.log.info('步骤2：打开业务建模-报表设计b,修改某个数据流，会打开新Tab')
        # 通过selenium定位元素再点击失败率很高，改用js
        js = "Array.from(document.querySelectorAll('span')).find(el => " \
             "el.textContent === '业务建模').click();"
        self.driver.exec_js(js)
        time.sleep(1)
        js = "Array.from(document.querySelectorAll('span')).find(el => " \
             "el.textContent === '报表设计').click();"
        self.driver.exec_js(js)
        time.sleep(5)

        js = "document.querySelector('.wujie_iframe').shadowRoot." \
             "querySelector('.arco-icon-edit').parentElement.click()"
        self.driver.exec_js(js)
        time.sleep(3)

        self.log.info('步骤3：打开插件管理页面')
        btn_xpath = '//*[text()="插件管理" and contains(@class, "arco-menu")]'
        self.driver.wait_click(btn_xpath)

        self.log.info('步骤4：找到基础运维插件，点击卸载按钮，确认【结果1】')
        self.driver.uninstall_plugin(self.plugin_name)

        self.log.info('步骤5： 查看之前基础运维插件打开的Tab页面【结果2】')
        tab_xpath1 = """//span[@class="tag-link" and text()="集群监控"]"""
        tab_xpath2 = """//span[@class="tag-link" and text()="报表设计"]"""
        with self.assertRaises(InvalidArgumentException):
            self.driver.wait_ele(tab_xpath1)
        with self.assertRaises(InvalidArgumentException):
            self.driver.wait_ele(tab_xpath2)

    def tearDown(self):
        try:
            if self.driver.is_plugin_intalled(self.plugin_name):
                self.driver.uninstall_plugin(self.plugin_name)
        except Exception as e:
            self.log.error(str(e))
        finally:
            self.driver.close()
            self.log.info('teardown run finished')
